package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-05-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    /**
     * 根据课程id获取课程里的章节、小节
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoNodeList(String courseId) {
        //根据课程id查询所有的章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<EduChapter>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(queryWrapper);
        // 根据课程id查询所有的小节
        QueryWrapper<EduVideo> query = new QueryWrapper<EduVideo>();
        query.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(query);

        //创建一级分类的集合 最后返回
        List<ChapterVo> chapterVoList = new ArrayList<ChapterVo>();

        for (EduChapter chapter : chapterList){
            //先把一级对象添加进chapterVoList
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoList.add(chapterVo);
            //创建接受二级分类的集合
            List<VideoVo> videoVoList = new ArrayList<VideoVo>();

            for (EduVideo eduVideo : videoList){
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduVideo,videoVo);
                videoVoList.add(videoVo);
            }
            chapterVo.setChildren(videoVoList);
        }
        return chapterVoList;
    }

    /**
     * 删除章节钱先判断是否此章节下还有小节 有则不允许删除
     * @param chapterId
     * @return
     */
    @Override
    public boolean removeChapter(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        List<EduVideo> videoList = videoService.list(queryWrapper);
        if (videoList.size()>0 || null != videoList){
            throw new GuliException(20001,"此章节下有小节，不能删除！");
        }else{
            int row = baseMapper.deleteById(chapterId);
            return row>0;
        }
    }

    /**
     * 根据课程id 删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }

}
